/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deatax.display;

import javax.media.opengl.GL2;

/**
 *
 * @author nrechten
 */
public class Light {

    protected static float[] AMBIENT = new float[]{ 0.4f, 0.4f, 0.4f, 1.0f };

    protected float[] position = new float[]{0.0f, 0.0f, 10.0f , 1.0f};
    protected float[] specular = new float[]{0.0f, 0.8f, 0.8f , 1.0f};
    protected float[] diffuse = new float[]{0.0f, 0.0f, 1.0f , 1.0f};

    private static boolean lights[] = new boolean[8];
    private static int count = 0;

    private int id;
    private boolean on = true;


    public Light(){
    }

    public float hax = 0;
    public void circle(){
        hax+=0.01f;

        position[0] = hax;
        position[1] = 0;
    }


    public void setup(GL2 gl){

        circle();
        if(on){
            enable(gl);
        } else {
            disable(gl);
        }
    }

    public void enable(GL2 gl){
        if(id < 0)
            id = getID(gl);
        if(id < 0) { System.out.println("No Lights avaliable"); return; }
//        init(gl);
        gl.glEnable(id);
        gl.glLightfv(id, GL2.GL_SPECULAR, specular ,0);
        gl.glLightfv(id, GL2.GL_DIFFUSE,  diffuse,0);
        gl.glLightfv(id, GL2.GL_POSITION, position,0);
        
    }

    public void disable(GL2 gl){
        releaseLight(gl,id);
//        deinit(gl);
    }

    protected static void init(GL2 gl){
        if(count == 0) { deinit(gl); return; }

        gl.glEnable(GL2.GL_LIGHTING);
        gl.glEnable(GL2.GL_COLOR_MATERIAL);
        gl.glShadeModel(GL2.GL_SMOOTH);
        gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, AMBIENT,0);
        gl.glColorMaterial(GL2.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE);
    }

    public static void deinit(GL2 gl){
        if(count != 0) return;

        gl.glDisable(GL2.GL_LIGHTING);
//        gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, AMBIENT,0);
    }

    protected static synchronized int getID(GL2 gl){
        for(int i = 0; i < 8; i++){
            if(!lights[i]) { 
                lights[i] = true;
                count++;
                return getLightID(i);
            }
        }
        return -1;
    }

    protected static void releaseLight(GL2 gl, int id){
        if(id > -1 && id < 8){
            lights[id] = false;
            count--;
        }
    }

    public static int getLightID(int id){
        switch(id){
            case 0: return GL2.GL_LIGHT0;
            case 1: return GL2.GL_LIGHT1;
            case 2: return GL2.GL_LIGHT2;
            case 3: return GL2.GL_LIGHT3;
            case 4: return GL2.GL_LIGHT4;
            case 5: return GL2.GL_LIGHT5;
            case 6: return GL2.GL_LIGHT6;
            case 7: return GL2.GL_LIGHT7;
            default: return GL2.GL_LIGHT0;
        }
    }

    /**
     * @return the on
     */
    public boolean isOn() {
        return on;
    }

    /**
     * @param on the on to set
     */
    public void setOn(boolean on) {
        this.on = on;
    }

}
